#include "../WinDraw/WinDraw.h"			//this includes the whole library. 
#include "MyEventHandler.h"				//includes the event handler (more in the header). 

//this file contains WinMain and the main loop. 

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
{
	//first and foremost, fix a mess with VStudio's idea of file systems. 
	//1: setup the file system, so that relative file paths are searched for files from the folder containing the executable file. 
	setupWorkDirectory(); 

	//2: create the main window and event handler
	MainWindow window;			//create the main window
	MyEventHandler eventHandler;	//create an event handler which will obtain input and rendering requests
	window.SetEventHandler(&eventHandler);	//attach the event handle to the main window
	//really create the main window using the Windows api (640 x 512 pixel, title is "PAC-MAN")
	window.Create(eventHandler.stageWidth()+176, eventHandler.stageHeight(), "PAC-MAN");		
	
	//3: perform the game loop. 
	const float secondsPerFrame = (1.f / 60.f); //We're aiming at 30 fps (1 second / 30 frames per second) = frame lenght
	Timer t;				//create a timer for the main loop
	while(window.IsAlive())		//closing a window will cause it to not be "alive" anymore. We'll loop until the window closes.  
	{
		t.TimeFromLast(); 
		window.DispatchInput(); //invokes "MyEventHandler::OnInput" as required.
		window.Redraw();			//invokes "MyEventHandler::OnRender" as required. 
		wait(secondsPerFrame - t.TimeFromLast()); //wait (frame seconds - seconds since last frame) to run at the desired fps and 
													//leave some processor time for everything else. 
		if(eventHandler.isDying()) 
		{
			wait(2.0f);
			window.Destroy();
		}
	}
	window.ClearEventHandler();		//detach the event handler, the window is closed anyway. 
	return 0; //as everything is on the stack for now, destructors will take care of the rest. 
}	